package com.fzrskj.cloud.free.core.utils;

import com.fzrskj.cloud.free.core.enums.SysUserGroupCodeEnum;
import com.fzrskj.cloud.free.sys.bean.User;
import com.fzrskj.cloud.free.sys.utils.CUserUtils;

import java.util.List;

/**
 * DataRangeUtils.
 *
 * @author zj.
 *         Created on 2018/8/7 0007.
 */
public class DataRangeUtils {
    /**
     * 数据访问权限Sql格式化.
     * @param sourceSql
     * @return
     */
    public static String formatRangeSql(String sourceSql) {
        return formatRangeSql(sourceSql, "userId");
    }

    /**
     * 数据访问权限Sql格式化.
     * @param sourceSql
     * @param columnName
     * @return
     */
    public static String formatRangeSql(String sourceSql, String columnName) {
        return formatRangeSql(sourceSql, columnName, true);
    }



    /**
     * 数据访问权限Sql格式化.
     * @param sourceSql
     * @param columnName
     * @return
     */
    public static String formatRangeSql(String sourceSql, String columnName, boolean needRule) {
        User user = CUserUtils.getUser(CloudContextUtils.getSession().getUID());
        if (null == user) {
            return sourceSql;
        }

        Integer groupCode = user.getGroupCode();
        if (SysUserGroupCodeEnum.ADMIN.getCode().equals(groupCode)) {
            return sourceSql;
        }

        sourceSql +=(needRule ? " and " : "") + columnName;

        if (SysUserGroupCodeEnum.CRM.getCode().equals(groupCode)) {
            sourceSql += "  in (select userId from user_account where crmId=" + user.getAccountId() + ") ";
            return sourceSql;
        }

        if (SysUserGroupCodeEnum.CSST.getCode().equals(groupCode)) {
            sourceSql += "  in (select userId from user_account where csstId=" + user.getAccountId() + ") ";
            return sourceSql;
        }
        sourceSql += " = -1 ";
        return sourceSql;
    }

    /**
     * 数据访问权限Sql格式化.
     * @param sourceSql
     * @param params
     */
    public static void formatRangeSql(StringBuilder sourceSql, List<Object> params) {
        formatRangeSql(sourceSql, " userId ", params);
    }

    public static void formatRangeSql(StringBuilder sourceSql, String columnName, List<Object> params) {
        formatRangeSql(sourceSql, columnName, params, true);
    }

    /**
     * 数据访问权限Sql格式化.
     * @param sourceSql
     * @param columnName
     * @param params
     */
    public static void formatRangeSql(StringBuilder sourceSql, String columnName, List<Object> params, boolean needRule) {
        User user = CUserUtils.getUser(CloudContextUtils.getSession().getUID());
        if (null == user) {
            return;
        }

        Integer groupCode = user.getGroupCode();
        if (SysUserGroupCodeEnum.ADMIN.getCode().equals(groupCode)) {
            return;
        }

        String rule;
        rule = needRule ? " and " : "";
        if (SysUserGroupCodeEnum.CRM.getCode().equals(groupCode)){
            sourceSql.append(rule).append(columnName)
                    .append(" in (select userId from user_account where crmId=?) ");
            params.add(user.getAccountId());
            return;
        }
        if (SysUserGroupCodeEnum.CSST.getCode().equals(groupCode)){
            sourceSql.append(rule).append(columnName)
                    .append(" in (select userId from user_account where csstId=?) ");
            params.add(user.getAccountId());
            return;
        }
        sourceSql.append(rule).append(columnName).append(" =? ");
        params.add(-1);
    }
}
